import cv2
import numpy as np
import os
a=0
num_x=[0,0,0,0,0,0,0,0,0]
num_y=[0,0,0,0,0,0,0,0,0]
cap = cv2.VideoCapture(0)# #调用摄像头‘0'一般是打开电脑自带摄像头，‘1'是打开外部摄像头（只有一个摄像头的情况）
#cap.set(3, 640)
#cap.set(4, 480)
index=0
imgPath="0.jpg"
#img=cv2.imread(imgPath)
while True:
    
    #frame=cv2.imread(imgPath)
    ret,frame = cap.read()
#     ret1,frame1 = cap.read()
    #print(frame.shape[1])
    gray1 = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    #gray = cv2.GaussianBlur(gray1, (5, 5), 1)
    
#    gray = cv2.medianBlur(gray, 3)
    
    #r, b = cv2.threshold(gray, 102, 90, cv2.THRESH_BINARY)
    
    
    #Bila = cv2.bilateralFilter(gray,0,20 0,5)
    #kernel = np.ones((3, 3), dtype=np.uint8)
    #k = np.ones((3, 3), np.uint8)
    
    #dilate = cv2.dilate(gray, kernel, 1)                 
    #image1_erode = cv2.erode(dilate, k)
    
    #wiki_equ=cv2.equalizeHist(image1_erode)
#    wiki_equ = cv2.equalizeHist(wiki_equ)
#    wiki_equ=cv2.equalizeHist(wiki_equ)
#    wiki_equ = cv2.bilateralFilter(wiki_equ, 0,     100,        15)
#    wiki_equ = cv2.blur(wiki_equ, (9, 9))     #均值滤波
#    wiki_equ = cv2.GaussianBlur(wiki_equ, (5, 5), 1)
#    ret,wiki_equ=cv2.threshold(wiki_equ, 200, 0, cv2.THRESH_BINARY);

#    wiki_equ = cv2.medianBlur(wiki_equ, 9)
#    wiki_equ = cv2.GaussianBlur(wiki_equ, (7, 7), 1)
#    r, wiki_equ = cv2.threshold(wiki_equ, 70, 255, cv2.THRESH_BINARY)
    #sobelxy=cv2.Sobel(image1_erode,cv2.CV_64F,1,1,3)
    #image1_erode = cv2.Canny(image1_erode, 90, 110
    arr1 = np.zeros([0, 2], dtype=int) # 创建一个0行, 2列的空数组
    circles = cv2.HoughCircles(gray1, cv2.HOUGH_GRADIENT, 1, 100, param1=100, param2=120, minRadius=0, maxRadius=170)
    if circles is not None:
        circles = np.uint16(np.around(circles))   # 4舍5入, 然后转为uint16
        for i in circles[0, :]:
            arr1 = np.append(arr1, (i[0], i[1]))            # arr1是圆心坐标的np数组
            # print(arr1)
            cv2.circle(frame, (i[0], i[1]), i[2], (0, 0, 255), 3)  # 轮廓
            #img5=frame[(i[0]-50):(i[0]+50),(i[1]-50):(i[1]+50)]
            #cv2.rectangle(img=frame,pt1=(i[0]-70,i[1]-70),pt2=(i[0]+70,i[1]+70),color=(0,0,255),thickness=2)
#            cv2.imshow("hhh",img5)
            #img5 = cv2.Canny(img5, 90, 110)
            
            #num_x[a]=i[0]
            #num_y[a]=i[1]
            #a=a+1
            #if(a==5):
                #a=0
               # num_x.sort()
               # num_y.sort()
            print("x_pos",i[0])
            print("y_pos",i[1])
#                 print("\n")
                #img5=frame[(num_x[4]-50):(num_x[4]+50),(num_y[4]-50):(num_y[4]+50)]
#                 cv2.imshow("hhh",img5)
            with open('./ring.txt', 'w+') as f:
                f.write(str(i[0])+'+'+str(i[1]))
#                    f.close()
#                     exit()
            cv2.circle(frame, (i[0], i[1]), 2, (0, 0, 0), 6)     # 圆心
    cv2.waitKey(1)


    #cv2.namedWindow("hhh", cv2.WINDOW_NORMAL)
    #cv2.resizeWindow("hhh",640,480)
    cv2.imshow("hhh",frame)
    
#200 代表应该检测到的行的最小长度
cap.release()#释放摄像头
cv2.destroyAllWindows()#关闭所有窗口